<?php

//文章模型

Class wx_menuModel extends Model{

    /**
     * 获得单条数据
     * @param $condition 条件
     * @return mixed
     */
    public function getwx_menuByOne($condition,$field){
        return $this->where($condition)->field($field)->find();
    }

    /**
     * 获得多条数据
     * @param $condition 条件
     * @param $field 字段
     * @param $order 排序
     * @param $limit 数量
     * @return mixed
     */
    public function getWx_menuByMore($condition,$field,$order,$limit){
        return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
    }

    /**
     * 添加菜单
     */
    public function addMenu($data){
        return $this->add($data);
    }

    /**
     * 查询总数
     */
    public function getMenuCount($condition=''){

        return $this->where($condition)->count();

    }


    /**
     * 修改
     * @param $condition 条件
     * @param $data 数据
     * @return mixed
     */
    public function updata($condition,$data){
        return $this->where($condition)->save($data);
    }

    /**
     * 整理提交过来的数据
     */
    public function addWXMenu($data){
         if(!is_array($data)){
                $res = ['status'=>'400','msg'=>"提交内容为空"];
                return $res;
                exit();
         }
         $menucount  = $this->getMenuCount();
         if($menucount>=3){
             $res = ['status'=>'400','msg'=>"菜单数量不能大于三"];
             return $res;
             exit();
         }
         $data['name'] = $data['name'];
         $data['type'] = $data['type'];
         $data['value'] = $data['value'];
         $reslut = $this->addMenu($data);
         if($reslut){
             $res = ['status'=>'200','msg'=>"菜单保存成功"];
             return $res;
             exit();
         }
         $res = ['status'=>'400','msg'=>"菜单保存失败"];
         return $res;
         exit();
    }

    /**
     * @param 添加菜单到wx
     */
    public function addMenuTowx(){
         $wx_menu = [];
         $menu = $this->getWx_menuList();
         $menu = $this->FormatMenu($menu);
         $wx_menu['button'] = $menu;
        if(!$menu){
             $res = ['status'=>'400','msg'=>"菜单为空"];
             return $res;
         }
         import('Class.wx',APP_PATH);
         $wx = new Wx();
         $access_token = $wx->getWxAccess_token();
         if(!$access_token){
             $res = ['status'=>'400','msg'=>"access_token获取失败"];
             return $res;
         }
         $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$access_token;
         $wx_menuStr = urldecode(json_encode($wx_menu));
         $res = GoCurl($url,"POST",$wx_menuStr);
         $res = json_decode($res,true);
         if($res['errcode']){
             $res = ['status'=>'400','msg'=>$res['errmsg']];
             return $res;
         }else{
             $res = ['status'=>'200','msg'=>"菜单生成成功"];
             return $res;
         }
    }

    /**
     * 格式化菜单列表
     * @return Format
     */
    public function FormatMenu($Menu){
          foreach($Menu as $k => $v){
               if($v['type']==1){
                     $Menu[$k]['type'] = "click";
                     $Menu[$k]['key'] = $v['value'];
               }else{
                     $Menu[$k]['type'] = "view";
                     $Menu[$k]['url'] = $v['value'];
               }
               $Menu[$k]['name'] = urlencode($v['name']);
               unset($Menu[$k]['id']);
               unset($Menu[$k]['value']);
               unset($Menu[$k]['wx_type']);
          }
          return $Menu;
    }


    public function getWx_menuList(){
        $wx_menuClick = array(
             1 => "点击",
             2 => "链接"
        );
        $wx_menu = $this->getWx_menuByMore();
        foreach($wx_menu as $k => $v){
              $wx_menu[$k]['wx_type'] = $wx_menuClick[$v['type']];
        }
        return $wx_menu;
    }
}

?>